package com.ellricky.baselib.ui.web;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;

import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;

import java.util.HashMap;

/**
 * Desc:   webview 相关工具类
 */

public class WebUtils {


    public static void initX5Web(Application app) {

        // 在调用TBS初始化、创建WebView之前进行如下配置
        HashMap<String, Object> map = new HashMap<>();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);

        QbSdk.setDownloadWithoutWifi(true);
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，
                // 否则表示x5内核加载失败，会自动切换到系统内核。
                Log.d("app", " onViewInitFinished is " + arg0);
            }

            @Override
            public void onCoreInitFinished() {
            }
        };
        //x5内核初始化接口
        QbSdk.initX5Environment(app.getApplicationContext(), cb);
    }


    public void reportCarsh(Context context) {
        //返回内核版本号
        QbSdk.getTbsVersion(context);
        // 返回内核辅助信息
        WebView.getCrashExtraMessage(context);
    }


    public static void jumpWeb(Activity context, Class cla, String title, String url, int requestCode) {
        jumpWeb(context, cla, title, url, "", requestCode);
    }


    public static void jumpWeb(Activity context, Class cla, String title, String url, String jsName, int requestCode) {
        Intent intent = new Intent(context, cla);
        intent.putExtra(Constant.KEY_TITLE, title);
        intent.putExtra(Constant.KEY_URL, url);
        intent.putExtra(Constant.KEY_JS_NAME, jsName);
        context.startActivityForResult(intent, requestCode);
    }


///////////////////////////////////////////////////////////////////////////
// 内部调用
///////////////////////////////////////////////////////////////////////////

    @SuppressLint("SetJavaScriptEnabled")
    public static void initWebviewSettings(WebView webView) {
        webView.getSettings().setLoadWithOverviewMode(true);
        WebSettings webSetings = webView.getSettings();
        webSetings.setJavaScriptEnabled(true);
        webSetings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetings.setSaveFormData(true);
        webSetings.setSavePassword(true);
        webSetings.setSupportZoom(true);
        webSetings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSetings.setBuiltInZoomControls(true);
        webSetings.setDefaultTextEncodingName("UTF-8");
        webSetings.setDomStorageEnabled(true);
        webSetings.setAppCacheMaxSize(104857600);
        webSetings.setUseWideViewPort(true);
    }


    public static boolean isEmpty(@Nullable CharSequence str) {
        return str == null || str.length() == 0 || "null".equalsIgnoreCase(str.toString());
    }


}
